<?php 
/**
 * @author          faithsmine@gmail.com
 * @copyright       www
 * @version         1.0
 * @name            post.action.php
 * @todo            发布新闻和注册产品
 * @final           2009-12-10
 */
!defined("IN_YCTOOL") && exit();
$userid = $user->userinfo['userid'];
$uname = $user->userinfo['username'];
$user->isLogin($userpathpre);

//获取POST/GET方法提交的变量
$operation      = isset($_GET['operation']) ? $_GET['operation'] : "";
$actionPost     = isset($_POST['actionPost']) ? $_POST['actionPost'] : '';
$id		= isset($_GET['id']) ? $_GET['id'] : $_POST['id'];
$type		= isset($_GET['type']) ? $_GET['type'] : $_POST['type']; 
$theurl  = "admin.php?action=$action&operation=$operation&type=$type";
$listurl = "admin.php?action=list&operation=$operation&type=$type";
$viewurl = "admin.php?action=view&operation=$operation&type=$type";
$plabel_arrs = db::getAll(DB_TABLEPRE . 'products_cls', '*', 'ctype=2 AND cset = 1 AND paid <> 0');

// |< ajax 处理 
$ajaxaction	= isset($_GET['ajaxaction']) ? $_GET['ajaxaction'] : "";
$ajax		= isset($_POST['ajax']) ? $_POST['ajax'] : "";
//不需要登录的处理
if($ajaxaction == 'ajax')
{
    if($ajax == 'chgpcls')
    { 
        $pclsid = isset($_POST['pclsid']) ? $_POST['pclsid'] : "";
 
        //file_put_contents('C:/b.txt', $ajax.'/'.$ajaxaction.' / '.$uroles.' / '. $uid);
        $pbrands = db::getAll(DB_TABLEPRE . 'products_cls', '*',
                   'paid = (SELECT paid FROM  '. DB_TABLEPRE . 'products_cls where clsid = ' . $pclsid. ')');
        if($pbrands)
        {
           echo 1;// serialize($pbrands);
        }
	exit;
    }
}
// ajax 处理 >|

$rs = array();   

switch ($operation)
{
    case 'news':
        $tableU = DB_TABLEPRE . 'news';
        $idkey = 'nid';
        break;
    case 'products':
        $tableU = DB_TABLEPRE . 'products';
        $idkey = 'pid';
        $pcls_arrs = db::getAll(DB_TABLEPRE . 'products_cls', '*', 'ctype=1 AND cset = 1 AND paid <> 0');

        break;
    case 'pcls':
        $tableU = DB_TABLEPRE . 'products_cls';
        $idkey = 'clsid';
        $pcls_arrs = db::getAll(DB_TABLEPRE . 'products_cls', '*', 'ctype=1 AND cset = 1 AND paid = 0');
        break;
    case 'plabel':
        $tableU = DB_TABLEPRE . 'products_cls';
        $idkey = 'clsid';
        $plabel_arrs = db::getAll(DB_TABLEPRE . 'products_cls', '*', 'ctype = 1  AND cset = 1 AND paid = 0');

        break;
    default:;
}
 
if ($_POST['actionPost'] == 'add')
{
    if ($type == 1)
    {      
        if (trim($_POST['ntitle']) != '')
        {
            $rs['ntitle']   = htmlspecialchars(trim($_POST['ntitle']));
        }
        else
        {
            func::alertMsg('请输入新闻标题');
        }
        if (trim($_POST['ncontent']) != '')
        {
            $rs['ncontent'] = htmlspecialchars(trim($_POST['ncontent']));
        }
        else
        {
            func::alertMsg('请输入新闻内容');
        }
        $rs['nposttime']    = NOWTIME;
        $rs['nuname']       = $uname;

    }
    else if($type == 2)
    {
        if (trim($_POST['pname']) != '')
        {
            $rs['pname']    = htmlspecialchars(trim($_POST['pname']));
        }
        else
        {
            func::alertMsg('请输入产品名称');
        }
        $picpath            = func::getFilePath2($userid, 'udata/', 'products');
 
        $rs['pclass']       = $_POST['pclass'];
        $rs['pbrand']       = $_POST['pbrand'];
        $rs['pstandard']    = htmlspecialchars(trim($_POST['pstandard']));
        if ($rs['pstandard'] == '')
        {
            func::alertMsg('请输入产品规格');
        }
        $rs['pprice']       = htmlspecialchars(trim($_POST['pprice']));

        if ($rs['pprice'] == '')
        {
            func::alertMsg('请输入产品价格');
        }
        $rs['poffers']      = htmlspecialchars(trim($_POST['poffers']));
        if ($rs['poffers'] == '')
        {
            func::alertMsg('请输入产品优惠价格');
        }
        $rs['pcount']      = htmlspecialchars(trim($_POST['pcount']));
        if ($rs['pcount'] == '')
        {
            func::alertMsg('请输入产品数量');
        }
        if(!$id)
        {
            // 图片上传
            $imgfiles = &$_FILES['pimage'];
       
            if ($imgfiles["size"] < 1)
            {
                func::alertMsg('请选择产品的形象图片');
            }
            if ($imgfiles["size"] > 1)
            { 	 	    
                if ($imgfiles["error"] == 1 || $imgfiles["error"] == 2)
                {
                    $error .= "图片文件" . $imgfiles["name"]. "”太大了，不能超过2M";
                }
                else if ($imgfiles["error"]  == 3 || $imgfiles["error"] == 4)
                {
                    $error .= "图片文件" . $imgfiles["name"] . "”上传错误，请重新上传";
                }
                else if ($imgfiles["size"] > 2000000)
                {
                    $error .= "图片文件" . $imgfiles["name"] . "”太大了，不能超过2M";
                }
                else if (!in_array($imgfiles["type"], array("image/gif", "image/jpeg", "image/pjpeg",   "image/png", "image/x-png")))
                {
                    $error .= "您上传的文件" . $imgfiles["name"] . "”不属于GIF、JPG、PNG中的任何格式";
                }
                if ($error)
                {
                    func::alertMsg($error);
                }	 
            }
            $newfile = time().md5(rand(0,9));
            $filename = $picpath . $newfile ;
            if ($imgfiles['size'])
            {	 
                func::imageResize($imgfiles["tmp_name"], 600, 400, $filename.'.jpg');
            } 
            $rs['pimage']       = $filename;
        }
        else
        {
            $rs['pimage'] = $_POST['existimg'];
        }       
        $rs['pdescrip']     = htmlspecialchars(trim($_POST['pdescrip']));
        $rs['sale']         = $_POST['sale'];
        if ($rs['pdescrip'] == '')
        {
            func::alertMsg('请输入产品描述');
        }
    }
    else if($type == 3)
    {
        if (trim($_POST['clsname']) != '')
        {
            $rs['clsname']   = htmlspecialchars(trim($_POST['clsname']));
        }
        else
        {
            func::alertMsg('请输入类别名称');
        }
        if ($_POST['paid'] != '')
        {
            $rs['paid'] = htmlspecialchars(trim($_POST['paid']));
        }
        else
        {
            func::alertMsg('请选择所属分类');
        }
        $rs['cset']    = 1;
        $rs['ctype']   = 1;
    }
    else if($type == 4)
    {
        if (trim($_POST['clsname']) != '')
        {
            $rs['clsname']   = htmlspecialchars(trim($_POST['clsname']));
        }
        else
        {
            func::alertMsg('请输入品牌名称');
        }
        if ($_POST['paid'] != '')
        {
            $rs['paid'] = htmlspecialchars(trim($_POST['paid']));
        }
        else
        {
            func::alertMsg('请选择所属分类');
        }
        $rs['cset']    = 1;
        $rs['ctype']   = 2;
    }

    if($id)
    {
	db::update($tableU, $rs, "$idkey ='$id'");	
    }
    else
    {
	db::insert($tableU, $rs);
	$id = db::insertID();
    }

    func::showMsg("操作成功", '../', $viewurl.'&id='.$id);
}
unset($rs);

if($id)
{
     $rs = db::getRow($tableU, '*', "$idkey = '$id'");	
}

?>
